﻿using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SqlServerCe;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.Licensing.Server
{
    public class LicenseDatabase : DbContext
    {
        private static string GetConnectionString()
        {
            var builder = new SqlCeConnectionStringBuilder();
            builder.AutoshrinkThreshold = 1;
            builder.DataSource = "LicenseDatabase.dat";
            builder.Encrypt = true;
            builder.Password = "UQOgA0XaKptTmorRknVx27tJ6nYUhfnC";
            return builder.ToString();
        }

        public LicenseDatabase()
            : base(GetConnectionString())
        {
            this.Database.ExecuteSqlCommand("SELECT NEWID()");
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Client Session
            modelBuilder.Entity<ClientSession>().Ignore(i => i.ExpiresOn);
            modelBuilder.Entity<ClientSession>().Ignore(i => i.Identifiers);
            modelBuilder.Entity<ClientSession>().Ignore(i => i.IdentifiersUsedToIdentify);
            modelBuilder.Entity<ClientSession>().Ignore(i => i.EndPoint);
        }

        public DbSet<ExecutionCount> ExecutionCounts { get; set; }
    }
}